using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using System;
using System.Collections.Generic;
namespace MicroMsg.UI.UserContrl
{
	public class HeadImgLoadService
	{
		private const string TAG = "HeadImgLoadService";
		public List<BaseHeadImgItem> mDataList = new List<BaseHeadImgItem>();
		private bool IsMutex;
		private EventWatcher mWatcherHeadImgAdd;
		private bool isHeadInit;
		public HeadImgLoadService(bool isMutex = true)
		{
			this.IsMutex = isMutex;
			this.GetHeaderInit();
		}
		public void Unit()
		{
			this.GetHeaderUnit();
			if (this.mDataList == null)
			{
				return;
			}
			this.mDataList.Clear();
			this.mDataList = null;
		}
		public void Add(BaseHeadImgItem headImgObject)
		{
			if (headImgObject == null)
			{
				return;
			}
			if (this.mDataList == null)
			{
				return;
			}
			if (this.mDataList.Contains(headImgObject))
			{
				return;
			}
			this.mDataList.Add(headImgObject);
		}
		public void Delete(BaseHeadImgItem headImgObject)
		{
			if (headImgObject == null)
			{
				return;
			}
			if (this.mDataList == null)
			{
				return;
			}
			if (!this.mDataList.Contains(headImgObject))
			{
				return;
			}
			this.mDataList.Remove(headImgObject);
		}
		private void GetHeaderInit()
		{
			if (this.isHeadInit)
			{
				return;
			}
			this.mWatcherHeadImgAdd = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerHeadImgAdd));
			EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
			Log.smethod_5("HeadImgLoadService", "registerEventWatcher EventConst.ON_MGR_HEAD_IMG_READY");
			this.isHeadInit = true;
		}
		private void GetHeaderUnit()
		{
			if (!this.isHeadInit)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
			Log.smethod_5("HeadImgLoadService", "removeEventWatcher EventConst.ON_MGR_HEAD_IMG_READY");
			this.isHeadInit = false;
		}
		private void onEventHandlerHeadImgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<string> listObject = evtArgs.getListObject<string>();
			if (listObject == null)
			{
				return;
			}
			if (this.mDataList == null)
			{
				return;
			}
			using (List<string>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (current != null && current.GetType() == typeof(string))
					{
						using (List<BaseHeadImgItem>.Enumerator enumerator2 = this.mDataList.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								BaseHeadImgItem current2 = enumerator2.get_Current();
								string text = BContactMgr.toBottleContactUserName(current2.UserName);
								if (text == current)
								{
									current2.HeadImgLoaded();
									if (this.IsMutex)
									{
										break;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
